package com.example.myapplication.utils;

import android.app.Activity;

import com.example.myapplication.MainActivity;
import com.kaopiz.kprogresshud.KProgressHUD;

import java.lang.ref.WeakReference;

public class LoadingDialogUtil {

    private static KProgressHUD kProgressHUD;

    public static void show(Activity activity) {
        show(activity, "正在加载...");
    }

    public static void show(Activity activity, String msg) {
        dismiss();
        WeakReference<Activity> weakReference = new WeakReference<>(activity);
        if (weakReference.get() != null) {
            kProgressHUD = KProgressHUD.create(weakReference.get())
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setLabel(msg).setCancellable(true)
                    .setAnimationSpeed(2)
                    .setDimAmount(0.0f)
                    .show();
        }
    }

    public static void dismiss() {
        if (kProgressHUD != null && kProgressHUD.isShowing()) {
            kProgressHUD.dismiss();
            kProgressHUD = null;
        }
    }
}
